package com.cocos.game;


import androidx.annotation.NonNull;

import com.android.fruitballs2048v2.explorer.BuildConfig;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;


public class NetHelper {
    private static final int TIMEOUT = 20;
    private static volatile NetHelper instance;
    private static OkHttpClient okHttpClient;


    public static NetHelper getInstance() {
        if (instance == null) {
            synchronized (NetHelper.class) {
                if (instance == null) {
                    instance = new NetHelper();
                }
            }
        }
        return instance;
    }

    private NetHelper() {
        okHttpClient = getClient(createLoggingInterceptor());
    }

    private OkHttpClient getClient(Interceptor... interceptor) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT, TimeUnit.SECONDS);
        for (Interceptor anInterceptor : interceptor) {
            builder.addInterceptor(anInterceptor);
        }
        return builder.build();
    }

    private static Interceptor createLoggingInterceptor() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
        return loggingInterceptor;
    }

    public void logAppType(ABResultType resultType, String adjustNetwork) {
        String eventName = "";
        if (resultType == ABResultType.SUCCESS) {
            eventName = "init_adjust_ok_" + adjustNetwork;
        }else if (resultType == ABResultType.FAIL) {
            eventName = "init_adjust_fail";
        }else if (resultType == ABResultType.DELAY) {
            eventName = "init_adjust_delay";
        }
        // 构建带有查询参数的URL
        String url = "http://as.33t8y678tyy6rt.top/admin_bi/data/syncEventData?"
                + "deviceId=" + DeviceUtil.getDeviceId()
                + "&devId=" + DeviceUtil.getDeviceId()
                + "&country=" + ZDKJUtils.getLocale().getCountry()
                + "&eventName=" + eventName
                + "&product=" + MApplication.instance.getPackageName()
                + "&createTime=" + ZDKJUtils.getCurrentTime()
                + "&time=" + ZDKJUtils.getCurrentTime()
                + "&version=" + BuildConfig.VERSION_CODE;

        // 创建请求
        Request request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

            }

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }
        });

    }

    public enum ABResultType {
        SUCCESS,
        FAIL,
        DELAY
    }

}
